package com.gopay.common.domain.bank.ccb.cbrmb.req;

import java.io.Serializable;

import org.apache.log4j.Logger;

import com.gopay.common.domain.bank.ccb.cbrmb.ByteData;
import com.gopay.common.util.cbrmb.MsgUtils;

/**
 * 
 * @ClassName: CbRmbQueryReqMsg
 * @Description: 查询报文数据
 * @author gd.cui
 * @date 
 */
public class CbRmbQueryReqMsg implements ByteData, Serializable {

    /**
     * @Fields serialVersionUID :
     */
    private static final long serialVersionUID = 1160861497105169291L;

    private static Logger logger = Logger.getLogger(CbRmbQueryReqMsg.class);

    public static final int MSG_NOTIFICATION_LENGTH = 65;
    /**
     * 交易类型  8888
     */
    private String tranType;

    /**
     * 通知方标识 建行发起固定-CCBS； 支付机构发起固定-GOPS;
     */
    private String senderId;
    /**
     * 交易流水 唯一标识每一笔通知，yyyyMMddHHmmss+3位序号
     */
    private String tranSn;
    /**
     * mac 存来盘/回盘文件SHA1校验值，用于内容有效性校验，40位长度大写表示
     */
    private String mac;

    private byte[] senderIdByte = new byte[4];

    private byte[] tranTypeByte = new byte[4];

    private byte[] tranSnByte = new byte[17];

    private byte[] macByte = new byte[40];
    
    public CbRmbQueryReqMsg() {
    }

    public CbRmbQueryReqMsg(byte[] source) {
        initPropertiesByBytes(source);
    }

    @Override
    public byte[] getByteData() {
        covertPropertiesToBytes();
        return MsgUtils.mergeBytes( this.tranTypeByte, this.senderIdByte,
                this.tranSnByte,  this.macByte);
    }

    private void covertPropertiesToBytes() {
    	
    	byte[] tranTypeSource = this.tranType.getBytes();
        int tranTypeLenth = tranTypeSource.length;
        MsgUtils.copyBytes(tranTypeSource, this.tranTypeByte, 1, tranTypeLenth, 1);
         
        byte[] senderIdSource = this.senderId.getBytes();
        int senderIdLenth = senderIdSource.length;
        MsgUtils.copyBytes(senderIdSource, this.senderIdByte, 1, senderIdLenth, 1);

        byte[] tranSnSource = this.tranSn.getBytes();
        int tranSnLenth = tranSnSource.length;
        MsgUtils.copyBytes(tranSnSource, this.tranSnByte, 1, tranSnLenth, 1);

        byte[] macSource = this.mac.getBytes();
        int macLenth = macSource.length;
        MsgUtils.copyBytes(macSource, this.macByte, 1, macLenth, 1);

    }

    @Override
    public void initPropertiesByBytes(byte[] source) {
        if (source != null && source.length >= MSG_NOTIFICATION_LENGTH) {
            MsgUtils.copyBytes(source, this.tranTypeByte, 1, 4, 1);
            MsgUtils.copyBytes(source, this.senderIdByte, 5, 4, 1);
            MsgUtils.copyBytes(source, this.tranSnByte, 9, 17, 1);
            MsgUtils.copyBytes(source, this.macByte, 26, 40, 1);

            this.tranType = new String(this.tranTypeByte);
            this.senderId = new String(this.senderIdByte);
            this.tranSn = new String(this.tranSnByte);
            this.mac = new String(this.macByte);
        } else {
            logger.error("init NotificationMsg from bytes error: the bytes not enough");
        }
    }

    public String getSenderId() {
        return senderId;
    }

    public void setSenderId(String senderId) {
        this.senderId = senderId;
    }

    public String getTranType() {
        return tranType;
    }

    public void setTranType(String tranType) {
        this.tranType = tranType;
    }


    public String getTranSn() {
        return tranSn;
    }

    public void setTranSn(String tranSn) {
        this.tranSn = tranSn;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

}